package org.ithang.concurrent;

import java.util.concurrent.locks.ReentrantLock;

import org.junit.After;
import org.junit.Test;

/**
 * Reentrant 重入锁，排他锁，共享，读写锁
 * @author Administrator
 *
 */
public class ReentrantLockTest {

	@Test
	public void aTest(){
		ReentrantLock lock=new ReentrantLock(true);//参数true表示创建的是公平锁，如果不传默认是非公平锁
		
		Thread ta=new Thread("ta"){
			@Override
			public void run() {
		       try{
				  lock.lock();
		          System.out.println("======执行ta业务逻辑=====");
		          Thread.sleep(3000);
		       }catch(Exception e){
		    	   e.printStackTrace();
		       }finally {
				  lock.unlock();
			   }
			}
		};
		ta.start();
		
		Thread tb=new Thread("tb"){
			@Override
			public void run() {
		       try{
				  lock.lock();
		          System.out.println("======执行tb业务逻辑=====");
		       }catch(Exception e){
		    	   e.printStackTrace();
		       }finally {
				  lock.unlock();
			   }
			}
		};
		tb.start();
		
	}
	
	
	@After
	public void after(){
		try{
	        Thread.sleep(6000);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}
